package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.SecurityControl;
import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.AlipayPayOrder;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.basic.WeixinPayOrder;
import cc.yugu.yhdw.common.domain.hdw.ExchangePacketOrder;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.basic.AgentService;
import cc.yugu.yhdw.webserver.service.basic.AlipayPayOrderService;
import cc.yugu.yhdw.webserver.service.basic.WeixinPayOrderService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangePacketOrderService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 换电套餐订单请求
 *
 * @Date:Created in 2018/7/31 10:49
 * @Author:
 * @Modified By: zxj 2018/7/31 10:49
 */
@Controller
@RequestMapping(value = "/security/hdw/exchange_packet_order")
public class ExchangePacketOrderController extends SecurityController {

    @Autowired
    ExchangePacketOrderService exchangePacketOrderService;
    @Autowired
    AgentService agentService;
    @Autowired
    AlipayPayOrderService alipayPayOrderService;
    @Autowired
    WeixinPayOrderService weixinPayOrderService;
    /**
     * 首页
     * @Param [model]
     * @Return void
     * @Author zxj 2018/7/31
     */
    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_12.getValue());
        model.addAttribute("PayTypeEnum", ConstEnum.PayType.values());
        model.addAttribute("StatusEnum", ExchangePacketOrder.Status.values());
        model.addAttribute("usedStatus", ExchangePacketOrder.Status.USED.getValue());
    }
    /**
     * 换电套餐订单翻页刷新
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/7/23
     */
    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(ExchangePacketOrder search, HttpServletRequest request) {
        if (search.getStatus() == null) {
            search.setStatus(ExchangePacketOrder.Status.USED.getValue());
        }else if(search.getStatus().equals(0)){
            search.setStatus(null);
        }
        /*
           查看换电套餐订单权限设置
           1.平台（总部）
           2.运营商
           3.代理商
         */
        int type = getSessionUser(request).getType();
        // 代理商
        if (type == User.Type.AGENCY.getValue()) {
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        // 运营商
        if (type == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        }
        return PageResult.successResult(exchangePacketOrderService.findPage(search));
    }
    /**
     * 支付宝支付套餐订单页面
     * @Param [model, id]
     * @Return void
     * @Author zxj 2018/7/31
     */
    @RequestMapping(value = "alipay_pay_order.htm")
    public void alipayPayOrder(Model model, String id) {
        model.addAttribute("sourceId", id);
    }
    /**
     * 支付宝支付套餐订单
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/7/31
     */
    @RequestMapping("alipay_pay_order_page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult alipayPayOrderPage(AlipayPayOrder search) {
        search.setSourceType(AlipayPayOrder.SourceType.EXCHANGE_PACKET_ORDER_PAY.getValue());
        search.setOrderStatus(AlipayPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(alipayPayOrderService.findPage(search));
    }
    /**
     * 微信支付套餐订单页面
     * @Param [model, id]
     * @Return void
     * @Author zxj 2018/7/31
     */
    @RequestMapping(value = "weixin_pay_order.htm")
    public void weixinPayOrder(Model model, String id) {
        model.addAttribute("sourceId", id);
    }
    /**
     * 微信支付套餐订单
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.json.PageResult
     * @Author zxj 2018/7/31
     */
    @RequestMapping("weixin_pay_order_page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult weixinPayOrderPage(WeixinPayOrder search) {
        search.setSourceType(WeixinPayOrder.SourceType.EXCHANGE_PACKET_ORDER_PAY.getValue());
        search.setOrderStatus(WeixinPayOrder.Status.SUCCESS.getValue());
        return PageResult.successResult(weixinPayOrderService.findPage(search));
    }
    /**
     * 查看
     * @Param [model, id]
     * @Return java.lang.String
     * @Author zxj 2018/7/31
     */
    @RequestMapping(value = "view.htm")
    public String view(Model model, String id) {
        ExchangePacketOrder entity = exchangePacketOrderService.find(id);

        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }

        return "/security/hdw/exchange_packet_order/view";
    }
    /**
     * 查看套餐订单详情
     * @Param [model, id]
     * @Return java.lang.String
     * @Author zxj 2018/7/31
     */
    @RequestMapping(value = "view_basic.htm")
    public String viewBasic(Model model, String id) {
        ExchangePacketOrder entity = exchangePacketOrderService.find(id);

        if(entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        model.addAttribute("PayTypeEnum", ConstEnum.PayType.values());
        model.addAttribute("StatusEnum", ExchangePacketOrder.Status.values());
        return "/security/hdw/exchange_packet_order/view_basic";
    }

}
